'use strict';

/* Controllers */
// signin controller
app.controller('LoginController', ['$scope', '$http', '$state', function ($scope, $http, $state) {
    $scope.user = {};
    $scope.authError = getAndRemoveStorage('go_to_login_msg');

    //如果存在
    $scope.tokenId = getTokenId();
    $scope.loginOut = function () {
        $http.get(beetle_auth_url + '/login/login_out?token_id=' + $scope.tokenId).success(function (data) {
            storage.clear()
        });
    };
    if ($scope.tokenId != '') {
        $scope.loginOut();
    }

    $scope.login = function () {
        $scope.authError = '';
        // Try to login
        $http.get(beetle_auth_url + '/login/?user_name=' + $scope.user.account + "&user_pass=" + $scope.user.password)
            .then(function (response) {
                var data = response.data;
                if (data.success == false) {
                    $scope.authError = data.msg;
                } else {
                    setTokenId(data.data.token_id);
                    $state.go('app.dashboard-v1');
                }
            }, function (x) {
                $scope.authError = 'Server Error';
            });
    };
}])
;